package com.hnkywl.sts.action.jhgl;

import com.hnkywl.sts.entity.jhgl.Bgjhhz;
import com.hnkywl.sts.entity.jhgl.Bgjhhzmx;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.jhgl.BgjhService;
import com.hnkywl.sts.service.jhgl.BgjhhzService;
import com.hnkywl.sts.service.jhgl.BgjhhzmxService;
import com.hnkywl.sts.service.organ.NbgsService;
import com.hnkywl.sts.service.sys.GnczService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ZHUCHG
 * Date: 13-6-30
 * Time: 下午2:56
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/jhgl/bgjhgyz")
public class BgjhgyzAction extends SimpleActionSupport {

    private Long id;

    private Integer zt;

    private Date date;
    //总公司
    private Nbgs zgs;
    //保供物资计划类型
    private String type;
    //保供计划单位Id
    private Long jhdwId;
    //供应站ID
    private Long gyzId;
    //计划年份
    private Integer jhnf;
    //计划月份
    private Integer jhyf;
    //物资编码
    private String wzbm;
    //计划说明
    private String sm;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //保供计划汇总
    private Bgjhhz bgjhhz;

    private Date startDate;

    private Date endDate;

    private String gyzsbcl;

    private List<Bgjhhzmx> bgjhhzmxList;

    private NbgsService nbgsService;
    private BgjhhzService bgjhhzService;
    private BgjhhzmxService bgjhhzmxService;
    private BgjhService bgjhService;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    private List<Map> gyzMxList;

    private String typeId;

    @Action("bgjhgyz")
    public String bgjhlr() {
        date = new Date();
        List<Nbgs> roots = nbgsService.findAllRootNbgs();
        if (!CollectionUtils.isEmpty(roots))
            zgs = roots.get(0);
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    @Action("bgjhgyzList")
    public void bgjhgyzList() {
        if (startDate != null)
            startDate = convertDate(startDate, true);
        if (endDate != null)
            endDate = convertDate(endDate, false);
        renderText(bgjhhzService.findGyzList(type, jhdwId, gyzId, jhnf, jhyf, wzbm,startDate,endDate, sm,gyzsbcl, page, rows));
    }

    @Action("bgjhgyzmxList")
    public void bgjhgyzmxList() {
        renderText(bgjhhzService.findMxList(id, zt));
    }

    @Action("bgjhgyz_content")
    public String bgjhgyzContent() {
        bgjhhz = bgjhhzService.find(id);
        return SUCCESS;
    }

    @Action("gyzcl")
    public void gyzcl() {
        if (bgjhhzmxList == null || bgjhhzmxList.size() == 0) {
            renderJson("{\"statusCode\":\"300\", \"message\":\"计划为空!\"}");
            return;
        }
        try {
            for(Bgjhhzmx mx:bgjhhzmxList){
                List<Bgjhhzmx> bgjhhzmxes = new ArrayList<Bgjhhzmx>();
                bgjhhzmxes.add(mx);
                if(mx.getThyy() !=null && !mx.getThyy().trim().equals("")){
                    bgjhhzmxService.gyzth(bgjhhzmxes);
                }else{
                    bgjhhzmxService.gyzcl(bgjhhzmxes);
                }
            }
            bgjhhzService.gyzclwc(bgjhhz.getId());
            renderJson("{\"statusCode\":\"200\", \"message\":\"操作成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

//    @Action("gyzth")
//    public void gyzth() {
//        if (bgjhhzmxList == null && bgjhhzmxList.size() == 0) {
//            renderJson("{\"statusCode\":\"300\", \"message\":\"计划为空!\"}");
//            return;
//        }
//        try {
//            bgjhhzmxService.gyzth(bgjhhzmxList);
//            bgjhhzService.gyzclwc(bgjhhz.getId());
//            renderJson("{\"statusCode\":\"200\", \"message\":\"操作成功!\"}");
//        } catch (Exception e) {
//            e.printStackTrace();
//            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
//        }
//    }

    @Action("bgjhgyzmx")
    public String bgjhgyzmx() {
        gyzMxList = bgjhService.findWzjhhzmxByBgjhIds(typeId, id);
        return SUCCESS;
    }

    //保供供应站detail
    @Action("bgjhgyzdetail")
    public void bgjhgyzdetail() {
        renderText(bgjhService.findWzjhhzmxByBgjhIdDetail(id, typeId, page, rows));
    }

    /**
     * @param date
     * @param flag true:当天00:00:00   false:第二天00:00:00
     * @return
     */
    private Date convertDate(Date date, boolean flag) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        if (!flag)
            gc.add(GregorianCalendar.DAY_OF_MONTH, 1);
        gc.set(GregorianCalendar.HOUR, 0);
        gc.set(GregorianCalendar.MINUTE, 0);
        gc.set(GregorianCalendar.SECOND, 0);
        return gc.getTime();
    }

    //=================setter,getter===================

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Bgjhhz getBgjhhz() {
        return bgjhhz;
    }

    public void setBgjhhz(Bgjhhz bgjhhz) {
        this.bgjhhz = bgjhhz;
    }

    public List<Bgjhhzmx> getBgjhhzmxList() {
        return bgjhhzmxList;
    }

    public void setBgjhhzmxList(List<Bgjhhzmx> bgjhhzmxList) {
        this.bgjhhzmxList = bgjhhzmxList;
    }

    //===================getter=====================

    public Date getDate() {
        return date;
    }

    public Nbgs getZgs() {
        return zgs;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    //===================setter=====================


    public void setZt(Integer zt) {
        this.zt = zt;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setJhdwId(Long jhdwId) {
        this.jhdwId = jhdwId;
    }

    public void setGyzId(Long gyzId) {
        this.gyzId = gyzId;
    }

    public void setJhnf(Integer jhnf) {
        this.jhnf = jhnf;
    }

    public void setJhyf(Integer jhyf) {
        this.jhyf = jhyf;
    }

    public void setWzbm(String wzbm) {
        this.wzbm = wzbm;
    }

    public void setSm(String sm) {
        this.sm = sm;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getGyzsbcl() {
        return gyzsbcl;
    }

    public void setGyzsbcl(String gyzsbcl) {
        this.gyzsbcl = gyzsbcl;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public List<Map> getGyzMxList() {
        return gyzMxList;
    }

    public void setGyzMxList(List<Map> gyzMxList) {
        this.gyzMxList = gyzMxList;
    }

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setZgs(Nbgs zgs) {
        this.zgs = zgs;
    }

    @Autowired
    public void setNbgsService(NbgsService nbgsService) {
        this.nbgsService = nbgsService;
    }

    @Autowired
    public void setBgjhhzService(BgjhhzService bgjhhzService) {
        this.bgjhhzService = bgjhhzService;
    }

    @Autowired
    public void setBgjhhzmxService(BgjhhzmxService bgjhhzmxService) {
        this.bgjhhzmxService = bgjhhzmxService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }
    @Autowired
    public void setBgjhService(BgjhService bgjhService) {
        this.bgjhService = bgjhService;
    }

}
